<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



/**
 * 根据订单Id查询订单信息
 * @param type 查询表
 * @param type 查询条件
 * @param type 查询字段
 * @return type
 */
//function getOrderById($model, $condition, $field) {
//     return M($model)->field($field)->where($condition)->find();
//     //echo M($model)->getLastSql();
//}

/**
 * 更新订单支付信息
 * @param type 查询表
 * @param type 查询条件
 * @param type 更新数据
 * @return type
 * 
 */
//function updateOrderPayInfoById($model, $condition, $data) {
//    return  M($model)->where($condition)->save($data);
//   // file_put_contents("./6666660000000.txt", var_export(M($model)->getLastSql(), true));
//}
//查询消费订单
function getScoreOrderById($model, $condition, $field) {
    $db_dsn = 'mysql://root:5tgbvfr4@127.0.0.1:3306/lvdi_ivote';
    $model = M($model, 'v_', $db_dsn);
    return $model->field($field)->where($condition)->find();
}

//更新消费订单
function updateScoreOrderById($model, $condition, $data) {
    //file_put_contents("./paylogs/1.txt", '111\t\n',FILE_APPEND);
    $db_dsn = 'mysql://root:5tgbvfr4@127.0.0.1:3306/lvdi_ivote';
    $model = M($model, 'v_', $db_dsn);
    return $model->where($condition)->save($data);
}

//更新积分操作
function updateHuiyuanScoreById($id) {
    $db_dsn = 'mysql://root:5tgbvfr4@127.0.0.1:3306/lvdi_ivote';
    $model = M('Score_order', 'v_', $db_dsn);//订单
    $qrmodel = M('Score_qrcode', 'v_', $db_dsn);//二维码
    $scoremodel = M('Score_scorelog', 'v_', $db_dsn);//积分日志
    $data = $model->where('id=' . $id)->find();//查询订单
    $where['qid'] = $data['qid'];
    $where['orderid'] = $data['id'];
    $rs = $scoremodel->where($where)->find();//判断积分记录 防止微信一直请求
    //file_put_contents("./paylogs/1888.txt", $scoremodel->getLastSql(),FILE_APPEND);
    if ($data['score'] > 0 && !$rs) {
        $qty = deductCredits($data['card_no'], $data['score']);
        $scorelog['qid'] = $data['qid'];
        $scorelog['orderid'] = $data['id'];
        $scorelog['score'] = $data['score'];
        $scorelog['huiyuantel'] = $data['huiyuantel'];
        $scorelog['huiyuanopenid'] = $data['openid'];
        $scorelog['ctime'] = date('Y-m-d H:i:s');
        $scoremodel->add($scorelog);
    }

    $udata['status'] = 2;
    $udata['utime'] = date('Y-m-d H:i:s');
    $qrmodel->where('id=' . $data['qid'])->save($udata); //更新二维码状态
}
